package com.paiai.lslease.action.apphouse;

import com.paiai.lslease.auto.model.AppLock;
import com.paiai.lslease.auto.model.AppLockTenant;
import com.paiai.lslease.base.BaseAction;
import com.paiai.lslease.model.AppLockTenantInfo;
import com.paiai.lslease.service.apphouse.AppLockService;
import com.paiai.lslease.service.apphouse.AppLockTenantService;
import com.paiai.lslease.service.appuser.AppSysUserService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class AppLockTenantAction extends BaseAction {

    private static final Logger logger = LoggerFactory.getLogger(AppLockTenantAction.class);

    private AppLockTenantService appLockTenantService;
    private AppLockService appLockService;

    private String operateUserId;
    private String tenantId;
    private String lockId;
    private String checkDate;
    private String deadDate;
    private String rentDays;
    private String userLockPassword;
    private String userLockName;
    private String userLockOpenMusic;
    private String item1;
    private String item2;
    private String item3;
    private String item4;
    private String item5;

    private String lockTenantId;

    public void setAppLockTenantService(AppLockTenantService appLockTenantService) {
        this.appLockTenantService = appLockTenantService;
    }

    public void setAppLockService(AppLockService appLockService) {
        this.appLockService = appLockService;
    }

    public void setOperateUserId(String operateUserId) {
        this.operateUserId = operateUserId;
    }

    public void setTenantId(String tenantId) {
        this.tenantId = tenantId;
    }

    public void setLockId(String lockId) {
        this.lockId = lockId;
    }

    public void setCheckDate(String checkDate) {
        this.checkDate = checkDate;
    }

    public void setDeadDate(String deadDate) {
        this.deadDate = deadDate;
    }

    public void setRentDays(String rentDays) {
        this.rentDays = rentDays;
    }

    public void setUserLockPassword(String userLockPassword) {
        this.userLockPassword = userLockPassword;
    }

    public void setUserLockName(String userLockName) {
        this.userLockName = userLockName;
    }

    public void setUserLockOpenMusic(String userLockOpenMusic) {
        this.userLockOpenMusic = userLockOpenMusic;
    }

    public void setLockTenantId(String lockTenantId) {
        this.lockTenantId = lockTenantId;
    }

    public void setItem1(String item1) {
        this.item1 = item1;
    }

    public void setItem2(String item2) {
        this.item2 = item2;
    }

    public void setItem3(String item3) {
        this.item3 = item3;
    }

    public void setItem4(String item4) {
        this.item4 = item4;
    }

    public void setItem5(String item5) {
        this.item5 = item5;
    }

    @Override
    public String toString() {
        return "AppLockTenantAction{" +
                "appLockTenantService=" + appLockTenantService +
                ", appLockService=" + appLockService +
                ", operateUserId='" + operateUserId + '\'' +
                ", tenantId='" + tenantId + '\'' +
                ", lockId='" + lockId + '\'' +
                ", checkDate='" + checkDate + '\'' +
                ", deadDate='" + deadDate + '\'' +
                ", rentDays='" + rentDays + '\'' +
                ", userLockPassword='" + userLockPassword + '\'' +
                ", userLockName='" + userLockName + '\'' +
                ", userLockOpenMusic='" + userLockOpenMusic + '\'' +
                ", lockTenantId='" + lockTenantId + '\'' +
                '}';
    }

    /**
     * 添加一个租客
     */
    public void insertLockTenant() {
        try {
            logger.info("添加一个租客======\n" + this.toString());
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户ID不是系统注册用户");
                return;
            }
            if (StringUtils.isEmpty(tenantId)) {
                sendFailureMsg("租客的ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(tenantId)) {
                sendFailureMsg("租客的ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(tenantId))) {
                sendFailureMsg("被添加的用户不是系统注册用户");
                return;
            }
            if (StringUtils.isEmpty(lockId)) {
                sendFailureMsg("设备ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(lockId)) {
                sendFailureMsg("设备ID格式错误");
                return;
            }
            if (!appLockService.lockExistById(Integer.parseInt(lockId))) {
                sendSuccesseMsg("设备不存在");
                return;
            }
            if (StringUtils.isEmpty(checkDate)) {
                sendFailureMsg("入住日期不能为空");
                return;
            }
            Date _checkDate;
            try {
                _checkDate = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).parse(checkDate);
            } catch (ParseException e) {
                e.printStackTrace();
                sendFailureMsg("入住日期格式错误，请参考yyyy-MM-dd");
                return;
            }
            if (StringUtils.isEmpty(deadDate)) {
                sendFailureMsg("到期日期不能为空");
                return;
            }
            Date _deadDate;
            try {
                _deadDate = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).parse(deadDate);
            } catch (ParseException e) {
                e.printStackTrace();
                sendFailureMsg("到期日期格式错误，请参考yyyy-MM-dd");
                return;
            }
            if (Integer.parseInt(deadDate.replace("-", "")) <= Integer.parseInt(checkDate.replace("-", ""))) {
                sendFailureMsg("租住日期不能小于或等于到期日期");
                return;
            }
            if (!StringUtils.isEmpty(rentDays)) {
                rentDays = "";
            }
            rentDays = (int) ((_deadDate.getTime() - _checkDate.getTime())/(1000 * 60 * 60 * 24)) + "";
            if (StringUtils.isEmpty(userLockName)) {
                userLockName = "租客" + tenantId + "的新设备";
            }
            if (!StringUtils.isEmpty(userLockOpenMusic) && !StringUtils.isNumeric(userLockOpenMusic)) {//用户传了开门提示音，但是格式不对
                sendFailureMsg("开门提示音数据错误，必须是一个数字");
                return;
            }
            AppLockTenant appLockTenantToInsert = new AppLockTenant();
            appLockTenantToInsert.setLockId(Integer.valueOf(lockId));
            appLockTenantToInsert.setTenantId(Integer.valueOf(tenantId));
            appLockTenantToInsert.setCheckDate(_checkDate);
            appLockTenantToInsert.setDeadDate(_deadDate);
            appLockTenantToInsert.setRentDays(Integer.valueOf(rentDays));
            appLockTenantToInsert.setUserLockName(userLockName);
            appLockTenantToInsert.setUserLockPassword(userLockPassword);
            appLockTenantToInsert.setUserLockOpenMusic(Integer.valueOf(userLockOpenMusic));
            AppLockTenant appLockTenant = appLockTenantService.insertLockTenant(appLockTenantToInsert);
            if (appLockTenant == null) {
                logger.info("appLockTenant == null");
                sendFailureMsg("添加租客失败");
                return;
            }
            sendSuccesseMsg(appLockTenant, "添加租客成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("添加租客失败");
        }
    }

    /**
     * 删除一个租客
     */
    public void deleteLockTenant() {
        try {
            logger.info("删除一个租客======\n" + this.toString());
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.userBelongLandlord(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户房东，不能删除该用户");
                return;
            }
            if (StringUtils.isEmpty(lockTenantId)) {
                sendFailureMsg("租客记录ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(lockTenantId)) {
                sendFailureMsg("租客记录ID格式错误");
                return;
            }
            if (appLockTenantService.selectLockTenant(Integer.parseInt(lockTenantId)) == null) {
                sendFailureMsg("租客记录不存在");
                return;
            }
            if (!appLockTenantService.deleteLockTenant(Integer.parseInt(lockTenantId))) {
                sendFailureMsg("删除租客记录失败");
                return;
            }
            sendSuccesseMsg("删除租客记录成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("删除租客记录失败");
        }
    }

    /**
     * 删除设备下的所有租客
     */
    public void deleteAllLockTenantByLockId() {
        try {
            logger.info("删除设备下的所有租客======\n" + this.toString());
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户ID不是系统注册用户");
                return;
            }
            if (StringUtils.isEmpty(lockId)) {
                sendFailureMsg("设备ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(lockId)) {
                sendFailureMsg("设备ID格式错误");
                return;
            }
            if (!appLockService.lockExistById(Integer.parseInt(lockId))) {
                sendSuccesseMsg("设备不存在");
                return;
            }
            if (!appLockTenantService.deleteAllLockTenantByLockId(Integer.parseInt(lockId))) {
                sendFailureMsg("删除设备下的所有租客失败");
                return;
            }
            sendSuccesseMsg("删除设备下的所有租客成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("删除设备下的所有租客失败");
        }
    }

    /**
     * 更新租客租房信息与设备自定义配置信息
     */
    public void modifyLockTenant() {
        try {
            logger.info("更新租客租房信息与设备自定义配置信息======\n" + this.toString());
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户ID不是系统注册用户");
                return;
            }
            if (StringUtils.isEmpty(lockTenantId)) {
                sendFailureMsg("租客记录ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(lockTenantId)) {
                sendFailureMsg("租客记录ID格式错误");
                return;
            }
            if (appLockTenantService.selectLockTenant(Integer.parseInt(lockTenantId)) == null) {
                sendFailureMsg("租客记录不存在");
                return;
            }
            if (StringUtils.isEmpty(deadDate) && StringUtils.isEmpty(userLockName)
                    && StringUtils.isEmpty(userLockPassword) && StringUtils.isEmpty(userLockOpenMusic)
                    && StringUtils.isEmpty(item1) && StringUtils.isEmpty(item2)
                    && StringUtils.isEmpty(item3) && StringUtils.isEmpty(item4)
                    && StringUtils.isEmpty(item5)) {
                sendFailureMsg("租客信息未更新");
                return;
            }
            AppLockTenant appLockTenantToUpdate = new AppLockTenant();
            appLockTenantToUpdate.setId(Integer.valueOf(lockTenantId));
//            if ((StringUtils.isEmpty(checkDate) && !StringUtils.isEmpty(deadDate)) || (!StringUtils.isEmpty(checkDate) && StringUtils.isEmpty(deadDate))) {
//                sendFailureMsg("租住开始日期与到期日期不匹配");
//                return;
//            }
            //先查询一次租客详情
            AppLockTenant appLockTenant_ = appLockTenantService.selectLockTenant(Integer.parseInt(lockTenantId));
            if (appLockTenant_ == null) {
                sendFailureMsg("更新租客信息失败");
                return;
            }
            checkDate = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(appLockTenant_.getCheckDate());
            if (!StringUtils.isEmpty(deadDate)) {
//                Date _checkDate;
//                try {
//                    _checkDate = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).parse(checkDate);
//                } catch (ParseException e) {
//                    e.printStackTrace();
//                    sendFailureMsg("验证入住日期格式错误，请参考yyyy-MM-dd");
//                    return;
//                }
                Date _deadDate;
                try {
                    _deadDate = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).parse(deadDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                    sendFailureMsg("验证到期日期格式错误，请参考yyyy-MM-dd");
                    return;
                }
                if (Integer.parseInt(deadDate.replace("-", "")) <= Integer.parseInt(checkDate.replace("-", ""))) {
                    sendFailureMsg("租住日期不能小于或等于到期日期");
                    return;
                }
                if (!StringUtils.isEmpty(rentDays)) {
                    rentDays = "";
                }
                rentDays = (int) ((_deadDate.getTime() - appLockTenant_.getCheckDate().getTime())/(1000 * 60 * 60 * 24)) + "";
//                appLockTenantToUpdate.setCheckDate(_checkDate);
                appLockTenantToUpdate.setDeadDate(_deadDate);
                appLockTenantToUpdate.setRentDays(Integer.valueOf(rentDays));
            }
            if (!StringUtils.isEmpty(userLockOpenMusic) && !StringUtils.isNumeric(userLockOpenMusic)) {//用户传了开门提示音，但是格式不对
                sendFailureMsg("开门提示音数据错误，必须是一个数字");
                return;
            }
            appLockTenantToUpdate.setUserLockName(userLockName);
            appLockTenantToUpdate.setUserLockPassword(userLockPassword);
            if (!StringUtils.isEmpty(userLockOpenMusic)) {
                appLockTenantToUpdate.setUserLockOpenMusic(Integer.valueOf(userLockOpenMusic));
            }
            appLockTenantToUpdate.setItem1(item1);
            appLockTenantToUpdate.setItem2(item2);
            appLockTenantToUpdate.setItem3(item3);
            appLockTenantToUpdate.setItem4(item4);
            appLockTenantToUpdate.setItem5(item5);
            AppLockTenant appLockTenant = appLockTenantService.modifyLockTenant(appLockTenantToUpdate);
            if (appLockTenant == null) {
                logger.info("appLockTenant == null");
                sendFailureMsg("更新租客信息失败");
                return;
            }
            sendSuccesseMsg(appLockTenant, "更新租客信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("更新租客信息失败");
        }
    }

    /**
     * 查询设备下所有的租客信息
     */
    public void selectAllLockTenant() {
        try {
            logger.info("查询设备下所有的租客信息======\n" + this.toString());
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户ID不是系统注册用户");
                return;
            }
            if (StringUtils.isEmpty(lockId)) {
                sendFailureMsg("设备ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(lockId)) {
                sendFailureMsg("设备ID格式错误");
                return;
            }
            if (!appLockService.lockExistById(Integer.parseInt(lockId))) {
                sendSuccesseMsg("设备不存在");
                return;
            }
            List<AppLockTenantInfo> appLockTenantList = appLockTenantService.selectAllLockTenant(Integer.parseInt(lockId));
            sendSuccesseMsg(appLockTenantList, "查询设备下所有租客信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("查询设备下所有租客信息失败");
        }
    }

    /**
     * 查询租客所有的设备信息
     */
    public void selectAllTenantLock() {
        try {
            logger.info("查询租客所有的设备信息======\n" + this.toString());
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户ID不是系统注册用户");
                return;
            }
            if (StringUtils.isEmpty(tenantId)) {
                sendFailureMsg("租客ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(tenantId)) {
                sendFailureMsg("租客ID格式错误");
                return;
            }
            if (!appSysUserService.userBelongTenant(Integer.parseInt(tenantId))) {
                sendSuccesseMsg("租客不存在");
                return;
            }
            List<AppLock> appLockList = appLockTenantService.selectAllTenantLock(Integer.parseInt(tenantId));
            sendSuccesseMsg(appLockList, "查询租客所有的设备信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("查询租客的所有设备信息失败");
        }
    }

    /**
     * 根据租客设备记录ID查询单条记录信息
     */
    public void selectSingleLockTenantById() {
        try {
            logger.info("根据租客设备记录ID查询单条记录信息======\n" + this.toString());
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户ID不是系统注册用户");
                return;
            }
            if (StringUtils.isEmpty(lockTenantId)) {
                sendFailureMsg("租客记录ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(lockTenantId)) {
                sendFailureMsg("租客记录ID格式错误");
                return;
            }
            AppLockTenant appLockTenant = appLockTenantService.selectLockTenant(Integer.parseInt(lockTenantId));
            if (appLockTenant == null) {
                sendFailureMsg("租客记录不存在");
                return;
            }
            sendSuccesseMsg(appLockTenant, "根据租客设备记录ID查询单条记录信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("根据租客设备记录ID查询单条记录信息失败");
        }
    }
}
